import LGXCOM from '../common/common.js'
import LXCoord from '../common/struct/coord.js'
import ActPkg from '../graph/action/rectaction.js'
import GraphFactory from '../graph/graphfactory.js'
import {LGXEnums}  from '../common/sysenum.js'


import globalConst from '../common/globalConst.js'

class RectSelectSvc {
    constructor(datamanger) {
        this.m_pDataMng = datamanger;
        this.m_pDrawAction = null;
        this.m_tLazyDelRegionGraphLst = new Array();
    }
    releaseSelRect() {
        let selFrame = this.m_pDataMng.getMulSelRectGraph();
        if (selFrame) {
            this.m_pDataMng.recordRepaintScope(selFrame);
            selFrame.m_layer.removeGraph(selFrame);
            this.m_tLazyDelRegionGraphLst.push(selFrame);
            this.m_pDataMng.setMulSelRectGraph(null);
        }

        if (this.m_pDrawAction) {
            this.m_pDrawAction = null;
        }
    }

    actLeftMouseDown(point) {
        if (this.m_pDataMng.getMulSelRectGraph() != null) {
            this.releaseSelRect();
        }

        this.changeRectSelAction();
        this.DoSelect(LGXEnums.LGXMouseMode.LMouseDown, point);
    }
    actLeftMouseUp(point) {
        if (this.m_pDataMng.getMulSelRectGraph() != null) {
            this.DoSelect(LGXEnums.LGXMouseMode.LMouseUp, point);
        }
    }
    actMouseMove(point) {
        if (this.m_pDataMng.getMulSelRectGraph() != null) {
            this.DoSelect(LGXEnums.LGXMouseMode.mouseMove, point);
        }
    }

    changeRectSelAction() {
        var factory = GraphFactory.GraphFactory;
        let mulSelRectGraph = factory.createGraphByEnums(0,LGXEnums.LGXGraphType.MultiSel_Rect_Type);

        let mapViewInfo = this.m_pDataMng.getMapViewInfo();
        mulSelRectGraph.getLineDef().color.clone(mapViewInfo.backColor.invert());
        this.m_pDrawAction = new ActPkg.RectGraphAction(mulSelRectGraph);
        mulSelRectGraph.m_gid = globalConst.GlobalConst.RectSel_GID;
        this.m_pDataMng.getLayerManager().addGraphToLayer(mulSelRectGraph, "", -1);
        this.m_pDataMng.setMulSelRectGraph(mulSelRectGraph);
        mulSelRectGraph.setSelect(1);
    }

    DoSelect(tMode, tPoint) {
        if (tMode == LGXEnums.LGXMouseMode.mouseMove) {
            this.m_pDataMng.clearFreshZone();
        }
        var mosEvt = { button:0, type:tMode, shiftDown:0, altDown:0, ctrlDown:0 };
        let selFrame = this.m_pDataMng.getMulSelRectGraph();
        this.m_pDataMng.recordRepaintScope(selFrame);
        selFrame.act2Mouse(mosEvt,tPoint);
        this.m_pDataMng.recordRepaintScope(selFrame);
    }

    finishSave() {
        this.releaseSelRect();
        for (let i = 0; i < this.m_tLazyDelRegionGraphLst.length; i++) {
            var graph = this.m_tLazyDelRegionGraphLst[i];
            graph = null;
        }
        this.m_tLazyDelRegionGraphLst.length = 0();
    }

}
export default { RectSelectSvc }